package com.bit101.components
{
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class ImageSlider extends Slider
	{
		[Embed(source='assets/tab/sliderBtnCls.png')]
		private var cls:Class;
		[Embed(source='assets/tab/leftSlider.png')]
		private var left:Class;
		[Embed(source='assets/tab/rightSlider.png')]
		private var right:Class;
		[Embed(source='assets/tab/tabSliderBg.png')]
		private var bg:Class;
		public var step:Number;
		public function ImageSlider(orientation:String=Slider.HORIZONTAL, parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, defaultHandler:Function=null)
		{
			super(orientation, parent, xpos, ypos, defaultHandler);
			addLeftRight();
		}
		private function addLeftRight():void{
			var bitmap:Bitmap = new bg();
			bitmap.y = 5;
			bitmap.x = -30;
			this.addChildAt(bitmap,0);
			var leftBtn:ImageEmbedButton = new ImageEmbedButton(this,-39,0,left,leftClick);
			var rightBtn:ImageEmbedButton = new ImageEmbedButton(this,272,0,right,rightClick);
		}
		private function leftClick(event:MouseEvent):void{
			if((this.value - step) < minimum){
				this.value = minimum
			}else{
				this.value -= step;
			}
			//this.value -= step;
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		private function rightClick(evnet:MouseEvent):void{
			//trace(value)
			if((this.value + step) > maximum){
				this.value = maximum
			}else{
				this.value += step;
			}
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		override protected function drawHandle():void
		{	
			_handle.addChild(new cls());
			positionHandle();
		}
		override protected function onDrag(event:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onSlide);
			if(_orientation == HORIZONTAL)
			{
				_handle.startDrag(false, new Rectangle(0, 0, _width - _handle.width, 0));
			}
			else
			{
				_handle.startDrag(false, new Rectangle(0, 0, 0, _height - _width));
			}
		}
		override protected function onSlide(event:MouseEvent):void
		{
			var oldValue:Number = _value;
			if(_orientation == HORIZONTAL)
			{
				_value = _handle.x / (width - _handle.width) * (_max - _min) + _min;
			}
			else
			{
				_value = (_height - _width - _handle.y) / (height - _width) * (_max - _min) + _min;
			}
			if(_value != oldValue)
			{
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		override protected function drawBack():void
		{
			
		}
		override protected function positionHandle():void
		{
			var range:Number;
			if(_orientation == HORIZONTAL)
			{
				range = _width - 83;
				_handle.x = (_value - _min) / (_max - _min) * range;
			}
			else
			{
				range = _height - _width;
				_handle.y = _height - _width - (_value - _min) / (_max - _min) * range;
			}
		}
	}
}